package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import main.Main;
import util.Constants;

public class MainJFrame extends JFrame{
	private static final long serialVersionUID = -8220498072054146367L;
	
	private JPanel center;
	
	private ActionListener actionListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("退出")){
				System.exit(0); 
			}
			else if(e.getActionCommand().equals("注销")){
				int rst = JOptionPane.showConfirmDialog(null, "确定吗？","注销确认", JOptionPane.YES_NO_OPTION);
				if(rst == JOptionPane.YES_OPTION){
					Main.user = null;
					MainJFrame.this.dispose();
					new LoginJDialog();
				}
			}
			else if(e.getActionCommand().equals("修改密码")){
				loadPanel(new ChangePasswordJPanel());
			}
			else if(e.getActionCommand().equals("入场")){
				loadPanel(new EntryJPanel());
			}
			else if(e.getActionCommand().equals("离场")){
				loadPanel(new LeaveJPanel());
			}
			else if(e.getActionCommand().equals("空闲车位")){
				loadPanel(new IdleParkJPanel());
			}			
			else if(e.getActionCommand().equals("在场车辆")){
				loadPanel(new StockCarJPanel());
			}
			else if(e.getActionCommand().equals("通行记录")){
				loadPanel(new PassBillJPanel());
			}
			else if(e.getActionCommand().equals("停车位")){
				loadPanel(new ParkJPanel());
			}
			else if(e.getActionCommand().equals("收费规则")){
				loadPanel(new ChargeRuleJPanel());
			}
			else if(e.getActionCommand().equals("用户管理")){
				loadPanel(new UserJPanel());
			}
			else if(e.getActionCommand().equals("月卡账单")){
				loadPanel(new MonthlyReceiptJPanel());
			}
			else if(e.getActionCommand().equals("停车费账单")){
				loadPanel(new ParkReceiptJPanel());
			}			
			else if(e.getActionCommand().equals("月卡管理")){
				loadPanel(new MonthlyCardJPanel());
			}		
			else if(e.getActionCommand().equals("收入统计")){
				loadPanel(new ReportJPanel());
			}			
		}
	};
	
	public MainJFrame(){
		Main.frame = this;
		setTitle("小阿停车场收费管理系统 - " + Main.user.name);
		setSize(1366,768);
	    setLocationRelativeTo(null);
	    setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		JMenuBar menubar = new JMenuBar();
		setJMenuBar(menubar);
		
		JMenu menu1 = new JMenu("系统(S)");
		JMenu menu2 = new JMenu("门岗操作(G)");
		JMenu menu3 = new JMenu("管理处操作(M)");
		menu1.setMnemonic(KeyEvent.VK_S);
		menu2.setMnemonic(KeyEvent.VK_G);
		menu3.setMnemonic(KeyEvent.VK_M);
		
		menubar.add(menu1);		
		if(Constants.gate.equals(Main.user.type)){
			menubar.add(menu2);
		}
		if(Constants.manager.equals(Main.user.type)){
			menubar.add(menu3);
		}
		
		JMenuItem menu11 = new JMenuItem("注销");
		JMenuItem menu12 = new JMenuItem("修改密码");
		JMenuItem menu13 = new JMenuItem("退出");		
//		menu11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
		menu11.addActionListener(actionListener);
		menu12.addActionListener(actionListener);
		menu13.addActionListener(actionListener);
				
		menu1.add(menu11);
		menu1.add(menu12);
		menu1.addSeparator();
		menu1.add(menu13);
		
		JMenuItem menu21 = new JMenuItem("入场");
		JMenuItem menu22 = new JMenuItem("离场");
		JMenuItem menu23 = new JMenuItem("空闲车位");
		JMenuItem menu24 = new JMenuItem("在场车辆");
		JMenuItem menu25 = new JMenuItem("通行记录");
		menu21.addActionListener(actionListener);
		menu22.addActionListener(actionListener);
		menu23.addActionListener(actionListener);
		menu24.addActionListener(actionListener);
		menu25.addActionListener(actionListener);
		menu2.add(menu21);
		menu2.add(menu22);
		menu2.add(menu23);
		menu2.add(menu24);
		menu2.add(menu25);

		JMenuItem menu31 = new JMenuItem("月卡管理");
		JMenuItem menu32 = new JMenuItem("月卡账单");
		JMenuItem menu33 = new JMenuItem("停车费账单");
		JMenuItem menu34 = new JMenuItem("收入统计");
		JMenuItem menu35 = new JMenuItem("收费规则");
		JMenuItem menu36 = new JMenuItem("用户管理");
		JMenuItem menu37 = new JMenuItem("停车位");

		menu31.addActionListener(actionListener);
		menu32.addActionListener(actionListener);
		menu33.addActionListener(actionListener);
		menu34.addActionListener(actionListener);
		menu35.addActionListener(actionListener);
		menu36.addActionListener(actionListener);
		menu37.addActionListener(actionListener);
		menu3.add(menu31);
		menu3.add(menu32);
		menu3.add(menu33);
		menu3.add(menu34);
		menu3.add(menu35);
		menu3.add(menu36);
		menu3.add(menu37);
		
		setLayout(new BorderLayout());
        center = new JPanel();
        add(center, BorderLayout.CENTER);

		setVisible(true);
	}
	
	private void loadPanel(JPanel panel){
		remove(center);
		center = panel;
		add(center, BorderLayout.CENTER);
		validate();
	}
	
}
